package code5;

import java.io.*;
import java.net.Socket;

public class ClientSendFile extends Thread {

    private String str = null;

    public ClientSendFile(String str){this.str=str;}

    @Override
    public void run(){

        DataOutputStream dos = null;
        DataInputStream dis = null;

        try {
            Socket s = new Socket("127.0.0.1", 1234);
            System.out.println("客户端已连接服务器文件传输端口！");
            String[] ss = str.split(" ");

            File file = new File(ss[1]);
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(new FileInputStream(file));

            byte[] barr = new byte[1024];
            int res = 0;
            while((res=dis.read(barr))!=-1){
                dos.write(barr,0,res);
                dos.flush();
            }
            System.out.println("文件"+file.getName()+"发送成功！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=dis){
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(null!=dos){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
